package com.gc.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.gc.business.domain.InventorySize;
import com.gc.business.domain.InventorySizePrice;
import com.gc.business.service.IInventorySizePriceService;
import com.gc.business.service.IInventorySizeService;
import com.gc.common.annotation.Log;
import com.gc.common.base.Result;
import com.gc.common.enums.BusinessType;
import com.gc.common.support.Convert;
import com.gc.common.utils.poi.ExcelUtil;
import com.gc.framework.util.ShiroUtils;
import com.gc.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存规格管理 控制层
 * 
 * @author hruiyan
 * @date 2020-11-10
 */
@Slf4j
@Controller
@RequestMapping("/business/inventorySize")
public class InventorySizeController extends BaseController {
    private String prefix = "business/inventorySize";
	
	private final IInventorySizeService inventorySizeService;

	private final IInventorySizePriceService inventorySizePriceService;

	@Autowired
	public InventorySizeController(IInventorySizeService inventorySizeService,
								   IInventorySizePriceService inventorySizePriceService) {
		this.inventorySizeService = inventorySizeService;
		this.inventorySizePriceService = inventorySizePriceService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:inventorySize:view")
	@GetMapping
	public String inventorySize(){
	    return prefix + "/inventorySize";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{sizeId}")
	public String edit(@PathVariable("sizeId") Long sizeId, ModelMap mmap){
		InventorySize inventorySize = inventorySizeService.getById(sizeId);
		mmap.put("inventorySize", inventorySize);
		return prefix + "/edit";
	}
	
	/**
	 * 查询库存规格管理列表
	 */
	//@RequiresPermissions("business:inventorySize:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(InventorySize inventorySize){
		startPage();
		inventorySize.setAttrId(getAttrId());
        List<InventorySize> list = inventorySizeService.list(inventorySize);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存库存规格管理
	 */
	@RequiresPermissions("business:inventorySize:add")
	@Log(title = "库存规格管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(InventorySize inventorySize){
		inventorySize.setAttrId(getAttrId());
		inventorySize.setCreateBy(ShiroUtils.getSysUser().getUserName());
		inventorySize.setCreateTime(DateUtil.date());
		inventorySizeService.save(inventorySize);
		return Result.success();
	}

	/**
	 * 修改保存库存规格管理
	 */
	@RequiresPermissions("business:inventorySize:edit")
	@Log(title = "库存规格管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(InventorySize inventorySize){
		inventorySizeService.updateById(inventorySize);
		return Result.success();
	}
	
	/**
	 * 删除库存规格管理
	 */
	@RequiresPermissions("business:inventorySize:remove")
	@Log(title = "库存规格管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		for (Long sizeId : longIds) {
			InventorySizePrice sizePrice = new InventorySizePrice().setSizeId(sizeId);
			List<InventorySizePrice> inventorySizePriceList = inventorySizePriceService.list(sizePrice);
			if (CollUtil.isNotEmpty(inventorySizePriceList)){
				return Result.failed("本物品存在出入库,不得删除!!!");
			}
			inventorySizeService.removeById(sizeId);
		}
		return Result.success();
	}

	/**
	 * 导出库存规格管理列表
	 */
	@RequiresPermissions("business:inventorySize:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(InventorySize inventorySize){
		List<InventorySize> list = inventorySizeService.list(inventorySize);
		ExcelUtil<InventorySize> util = new ExcelUtil<>(InventorySize.class);
		util.exportExcel(list, "inventorySize");
		return Result.success();
	}
}
